﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace NovelDownload.Common
{
    public static class UiOperate
    {
        private static object locker = new object();

        /// <summary>
        /// 搜索结果列表添加数据
        /// </summary>
        /// <param name="frm"></param>
        /// <param name="dataGridView"></param>
        /// <param name="novelSite"></param>
        /// <param name="novels"></param>
        public static void SearchResultAddData(Form frm, DataGridView dataGridView, string novelSite, List<NovelInfo> novels)
        {
            if (novels == null) { return; }
            lock (locker)
            {
                Action action = () =>
                {
                    foreach (var novel in novels)
                    {
                        DataGridViewRow row = new DataGridViewRow();
                        row.Cells.Add(new DataGridViewTextBoxCell() { Value = novelSite });
                        row.Cells.Add(new DataGridViewLinkCell() { Value = novel.NovelName, Tag = novel.NovelLink });
                        row.Cells.Add(new DataGridViewTextBoxCell() { Value = novel.LatestChapter });
                        row.Cells.Add(new DataGridViewTextBoxCell() { Value = novel.Author });
                        row.Cells.Add(new DataGridViewTextBoxCell() { Value = novel.UpdateTime });
                        row.Cells.Add(new DataGridViewButtonCell() { Value = "阅读", Tag = novel.NovelLink });
                        row.Cells.Add(new DataGridViewButtonCell() { Value = "下载", Tag = novel.NovelLink });
                        dataGridView.Rows.Add(row);
                    }
                };

                frm.BeginInvoke(action);
            }
        }


        ///// <summary>
        ///// 章节列表（listview）添加数据
        ///// </summary>
        ///// <param name="frm"></param>
        ///// <param name="listView"></param>
        ///// <param name="chapters"></param>
        //public static void ChapterListviewAddData(Form frm, ListView listView, List<ChapterInfo> chapters)
        //{
        //    Action action = () =>
        //    {
        //        listView.Items.Clear();
        //        foreach (var chapter in chapters)
        //        {
        //            ListViewItem lvi = new ListViewItem();
        //            lvi.Text = chapter.ChapterTitle;
        //            lvi.Tag = chapter.ChapterLink;
        //            listView.Items.Add(lvi);
        //        }
        //    };
        //    frm.BeginInvoke(action);
        //}


        /// <summary>
        /// 章节列表（datagridview）添加数据
        /// </summary>
        /// <param name="frm"></param>
        /// <param name="gridView"></param>
        /// <param name="chapters"></param>
        public static void ChapterListAddData(Form frm, DataGridView gridView, List<ChapterInfo> chapters)
        {
            Action action = () =>
            {
                gridView.Rows.Clear();
                int c = chapters.Count;
                int i = 0;
                for (; i < c; i++)
                {
                    DataGridViewRow row = new DataGridViewRow();
                    row.Cells.Add(new DataGridViewTextBoxCell() { Value = chapters[i].ChapterTitle, Tag = chapters[i].ChapterLink });
                    if ((i + 1) < c)
                    {
                        row.Cells.Add(new DataGridViewTextBoxCell() { Value = chapters[i + 1].ChapterTitle, Tag = chapters[i + 1].ChapterLink });
                    }
                    gridView.Rows.Add(row);
                    i = i + 1;
                }
            };
            frm.BeginInvoke(action);

        }



        /// <summary>
        /// 显示阅读面板
        /// </summary>
        /// <param name="frm"></param>
        /// <param name="tabPage"></param>
        /// <param name="panel"></param>
        /// <param name="richTextBox"></param>
        /// <param name="contentStr"></param>
        public static void ShowReadingWindow(Form frm, TabPage tabPage,Panel panel, RichTextBox richTextBox,string contentStr)
        {
            Action action = () =>
            {
                panel.Width = tabPage.Width;
                richTextBox.Font = new Font("宋体", 12);
                richTextBox.BackColor = Color.Honeydew;
                richTextBox.Text = contentStr;
                panel.Visible = true;
            };
            frm.BeginInvoke(action);
        }

    }
}
